package cn.steam;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author cxx on 2022/06/22
 **/
@ApiModel("机器人库存详情")
@Data
public class RobotStockDetailsDTO {

    @ApiModelProperty("库存中物品")
    private List<ItemDetails> stockItems;

    @ApiModelProperty("组件中物品")
    private List<ItemDetails> storageItems;


    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    @Builder
    public static class ItemDetails {

        @ApiModelProperty("marketHashName")
        private String marketHashName;

        @ApiModelProperty("箱子名称")
        private String name;

        @ApiModelProperty("剩余数量")
        private int number;

    }

    /**
     * @return K:marketHashName V:总数量
     */
    public Map<String, Integer> allStockMap() {

        ArrayList<ItemDetails> allItems = new ArrayList<>();

        if (!CollectionUtils.isEmpty(stockItems)) {
            allItems.addAll(stockItems);
        }
        if (!CollectionUtils.isEmpty(storageItems)) {
            allItems.addAll(storageItems);
        }

        return allItems.stream().collect(Collectors.groupingBy(ItemDetails::getMarketHashName))
                .entrySet().stream().collect(Collectors.toMap(
                        Map.Entry::getKey,
                        entry -> entry.getValue().stream().map(ItemDetails::getNumber).reduce(Integer::sum).orElse(0)));
    }


    public static RobotStockDetailsDTO mock() {

        RobotStockDetailsDTO dto = new RobotStockDetailsDTO();
        dto.setStockItems(
                Arrays.asList(
                        ItemDetails.builder().marketHashName("Operation Breakout Weapon Case")
                                .name("冬季大行动").number(3).build(),
                        ItemDetails.builder().marketHashName("Huntsman Weapon Case")
                                .name("蛇嗜武器箱").number(2).build()
                ));

        dto.setStorageItems(
                Arrays.asList(
                        ItemDetails.builder().marketHashName("Operation Breakout Weapon Case")
                                .name("手套武器箱").number(3).build(),
                        ItemDetails.builder().marketHashName("Huntsman Weapon Case")
                                .name("突围大行动").number(2).build()
                ));

        return dto;

    }

}
